//    Dhammapada
//    Copyright (C) 2007 Sakya
//    sakya_tg@yahoo.it
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#include <pspkernel.h>
#include <pspsdk.h>
#include <stdio.h>
#include <oslib/oslib.h>

#include "mainmenu.h"
#include "dhammapada.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Globals:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int indexMenuRetValue = 0;
static int exitFlagIndexMenu = 0;
struct menuElements tMenu;
struct settings *lUserSettings;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Select:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menuElSelect(){
    lUserSettings->lastID = tMenu.selected;
    lUserSettings->lastSection = 0;
    indexMenuRetValue = MODE_VIEWCATEGORY;
    lUserSettings->intValue01 = tMenu.selected;
    lUserSettings->intValue02 = tMenu.first;
    lUserSettings->previousMode = MODE_INDEX;
    exitFlagIndexMenu = 1;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Back to main menu:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menuCancel(){
    indexMenuRetValue = MODE_MAINMENU;
    lUserSettings->intValue01 = 1;
    lUserSettings->intValue02 = 0;
    exitFlagIndexMenu = 1;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main menu:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int indexMenu(struct settings *userSettings){
    char buffer[262];
    struct menuElement tMenuEl;
    int i = 0;
    
    lUserSettings = userSettings;
    indexMenuRetValue = -1;
    //Load images:
    sprintf(buffer, "%s/images/bkg_default.png", userSettings->ebootPath);
    OSL_IMAGE *bkg = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!bkg)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/toolbar.png", userSettings->ebootPath);
    OSL_IMAGE *indexToolbar = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!indexToolbar)
        errorLoadImage(buffer);

    //Build menu:
    tMenu.yPos = 45;
    tMenu.xPos = 65;
    tMenu.width = 350;
    tMenu.height = 200;
    tMenu.cancelFunction = menuCancel;
    sprintf(buffer, "%s/images/menubkg.png", userSettings->ebootPath);
    tMenu.background = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!tMenu.background)
        errorLoadImage(buffer);
    tMenu.interline = 3;
    tMenu.first = 0;
    tMenu.selected = 0;
    tMenu.numberOfElements = categoriesIndex.count;
    tMenu.maxNumberVisible = 8;
    
    for (i=0; i<categoriesIndex.count; i++){
        strcpy(tMenuEl.name, categoriesIndex.cat[i].fileName);
        sprintf(tMenuEl.text, "%i. %s", i+1, categoriesIndex.cat[i].title);
        strcpy(tMenuEl.helpText, categoriesIndexOriginal.cat[i].title);
        tMenuEl.triggerFunction = menuElSelect;
        tMenu.elements[i] = tMenuEl;
    }
    tMenu.selected = lUserSettings->intValue01;
    tMenu.first = lUserSettings->intValue02;

    exitFlagIndexMenu = 0;
    while(!osl_quit && !exitFlagIndexMenu){
        oslStartDrawing();

        oslDrawImageXY(bkg, 0, 0);
        oslDrawImageXY(indexToolbar, 0, 272 - indexToolbar->sizeY);
        drawMenu(&tMenu);
        oslReadKeys();
        processMenuKeys(&tMenu);

    	oslEndDrawing();
    	oslSyncFrame();
    	oslAudioVSync();
    }
    //unLoad images:
    clearMenu(&tMenu);
    oslDeleteImage(bkg);
    oslDeleteImage(indexToolbar);
    return indexMenuRetValue;
}

